<?php

namespace App\Library;

use App\Models\Integrallist;

/**
 * 处理积分
 */
class HandleIntegral
{
    //获取要过期的积分
    public static function entry($user_id)
    {
        //今年年初的时间戳
        $start_time_this = strtotime(date('Y-1-1'));
        //今年年底12月31号的时间戳
        $due_time_this = strtotime(date('Y-12-31 23:59:59'));
        //去年年初的时间戳
        $start_time_last = strtotime('-1 year', $start_time_this);
        //去年年底12月31号的时间戳
        $due_time_last = strtotime('-1 year', $due_time_this);
        //去年年初到年底获得的积分
        $integral_get = Integrallist::where(['userID' => $user_id, 'type' => 1])
            ->whereBetween('time', [$start_time_last, $due_time_last])->sum('integral');
        //去年年初到今年年底使用的积分
        $integral_use = Integrallist::where(['userID' => $user_id, 'type' => 2])
            ->whereBetween('time', [$start_time_last, $due_time_this])->sum('integral');

        if ($integral_get >= $integral_use) {
            return ($integral_get - $integral_use);
        } else {
            return 0;
        }
    }
}
